﻿using System;
using System.Collections.Generic;
using System.Runtime.InteropServices;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Interop;
using System.Windows.Media;
using TeBigScreenWpf.Pages;

namespace TeBigScreenWpf
{
    /// <summary>
    /// Interaction logic for HotKeyWindow.xaml
    /// </summary>
    public partial class HotKeyWindow : Window
    {
        #region [HotKeys]

        [DllImport("User32.dll")]
        private static extern bool RegisterHotKey(IntPtr hWnd, int id, uint fsModifiers, uint vk);

        [DllImport("User32.dll")]
        private static extern bool UnregisterHotKey(IntPtr hWnd, int id);

        private HwndSource m_hWndSource;
        private const int HOTKEY_ID_01 = 9001;
        private const int HOTKEY_ID_02 = 9002;
        private const int HOTKEY_ID_03 = 9003;
        private const int HOTKEY_ID_04 = 9004;
        private const int HOTKEY_ID_05 = 9005;

        private const uint KEY_NUM1 = 0x31;
        private const uint KEY_NUM2 = 0x32;
        private const uint KEY_NUM3 = 0x33;
        private const uint KEY_NUM4 = 0x34;
        private const uint KEY_NUM5 = 0x35;

        private Dictionary<int, uint> m_hotkeyList = new Dictionary<int, uint>
        {
            {HOTKEY_ID_01, KEY_NUM1},
            {HOTKEY_ID_02, KEY_NUM2},
            {HOTKEY_ID_03, KEY_NUM3},
            {HOTKEY_ID_04, KEY_NUM4},
            {HOTKEY_ID_05, KEY_NUM5},
        };

        protected override void OnSourceInitialized(EventArgs e)
        {
            var helper = new WindowInteropHelper(this);
            helper.EnsureHandle();

            m_hWndSource = HwndSource.FromHwnd(helper.Handle);

            // 添加处理程序
            if (m_hWndSource != null)
                m_hWndSource.AddHook(HookWndProc);

            RegisterHotKey();
        }

        protected override void OnClosed(EventArgs e)
        {
            m_hWndSource.RemoveHook(HookWndProc);
            m_hWndSource = null;
            UnregisterHotKey();
            base.OnClosed(e);
        }

        private void RegisterHotKey()
        {
            var helper = new WindowInteropHelper(this);

            const uint MOD_CTRL = 0x0002;

            foreach (var kv in m_hotkeyList)
            {
                if (!RegisterHotKey(helper.Handle, kv.Key, MOD_CTRL, kv.Value))
                {
                    // handle error
                }
            }
        }

        private void UnregisterHotKey()
        {
            var helper = new WindowInteropHelper(this);

            foreach (var kv in m_hotkeyList)
            {
                UnregisterHotKey(helper.Handle, kv.Key);
            }
        }

        private IntPtr HookWndProc(IntPtr hwnd, int msg, IntPtr wParam, IntPtr lParam, ref bool handled)
        {
            const int WM_HOTKEY = 0x0312;
            switch (msg)
            {
                case WM_HOTKEY:
                    handled = OnScheme(wParam.ToInt32());

                    break;
            }
            return IntPtr.Zero;
        }

        #endregion [HotKeys]

        public static Dictionary<string, Page> pageNewList = new Dictionary<string, Page>()
        {
            {"主屏左3D", new Page16()  },
            {"主屏中", new Page4()  },
            {"主屏右", new Page11()  },
            {"工作量统计", new Page6()  },
            {"航拍页面",   new Page14() },
            {"警情布局1",  new Page2()  },
            {"警情布局2",  new Page8()  },
            {"警情布局3",  new Page3()  },
            {"地图布局1",  new Page5()  },
            {"地图布局2",  new Page7()  },
            {"分析布局1",  new Page13()  },
            {"分析布局2",  new Page15()  },
            {"分析布局3",  new Page20()  },
            {"地图分析1",  new Page9()  },
            {"地图分析2",  new Page17()  },
            {"图例页面",  new Page1()  },
            {"主屏右2", new Page18()  },
        };

        private List<Window> m_wOpenWindows = new List<Window>();

        private List<Window> m_wTempWindows = new List<Window>();

        public HotKeyWindow()
        {
            InitializeComponent();

            Left = 0;
            Top = 0;
            Width = 0;
            Height = 0;
            WindowStartupLocation = WindowStartupLocation.Manual;
            WindowStyle = WindowStyle.None;
            ShowInTaskbar = false;
            ShowActivated = false;
            Background = Brushes.Transparent;
            AllowsTransparency = true;

            OnSchemeDefault();
        }

        private void Window_Closed(object sender, EventArgs e)
        {
            if (sender is Window w)
            {
                m_wOpenWindows.Remove(w);

                if (m_wOpenWindows.Count == 0)
                {
                    Close();
                }
            }
        }

        private void AddCloseHandler(Window w)
        {
            w.Closed += Window_Closed;

            m_wOpenWindows.Add(w);
        }

        private void RemoveCloseHandler(Window w)
        {
            w.Closed -= Window_Closed;

            w.Close();
        }

        private void CreateMainWindow()
        {
            var w = new MainWindow();

            w.ShowFullScreen();

            //w.Show();

            AddCloseHandler(w);
        }

        private void CreatePageWindow(string key, int screen, bool fullscreen = true)
        {
            var value = pageNewList[key];
            var w = new QuickWindow(value, key, screen);

            if (fullscreen)
                w.ShowFullScreen(screen);
            else
                w.Show();

            AddCloseHandler(w);
        }

        private void CreateCrossWindow(IList<PageItem> list, bool fullscreen = true, bool title = true)
        {
            var w = new CrossWindow(list, 1, title);

            if (fullscreen)
                w.ShowFullScreen();
            else
                w.Show();

            AddCloseHandler(w);
        }

        private bool OnScheme(int id)
        {
            switch (id)
            {
                case HOTKEY_ID_01:
                    OnScheme(OnScheme1);
                    return true;

                case HOTKEY_ID_02:
                    OnScheme(OnScheme2);
                    return true;

                case HOTKEY_ID_03:
                    OnScheme(OnScheme3);
                    return true;

                case HOTKEY_ID_04:
                    OnScheme(OnScheme4);
                    return true;

                case HOTKEY_ID_05:
                    OnScheme(OnScheme5);
                    return true;
            }

            return false;
        }

        private void OnScheme(Action action)
        {
            m_wTempWindows.Clear();
            m_wTempWindows.AddRange(m_wOpenWindows);
            m_wOpenWindows.Clear();

            action();

            foreach (var item in m_wTempWindows)
            {
                RemoveCloseHandler(item);
            }
        }

        private void OnSchemeDefault()
        {
            //OnScheme1();
            OnScheme5();
        }

        private void OnScheme1()
        {
            CreateMainWindow();

            CreatePageWindow("工作量统计", 3);
        }

        private void OnScheme2()
        {
            CreatePageWindow("主屏左3D", 0);
            CreatePageWindow("地图布局2", 1);
            CreatePageWindow("分析布局1", 2);
            CreatePageWindow("分析布局2", 3);
        }

        private void OnScheme3()
        {
            CreatePageWindow("地图布局1", 0);
            CreatePageWindow("警情布局1", 1);
            CreatePageWindow("警情布局2", 2);
            //CreatePageWindow("警情布局3", 3);
        }

        private void OnScheme4()
        {
            CreatePageWindow("分析布局3", 0);
            CreatePageWindow("地图分析2", 1);
            CreatePageWindow("分析布局2", 2);
        }

        private List<PageItem> Items1 = new List<PageItem>
            {
                new PageItem {
                    Page = new Page16(),
                    Alternatives = new List<Page>
                    {
                        new Page8(),
                        new Page3(),
                        new Page13(),
                    }
                },
                new PageItem {
                    Page = new Page4(),
                    Alternatives = new List<Page>
                    {
                        new Pages.PageSingle.Page1(),
                        new Pages.PageSingle.Page2(),
                        new Pages.PageSingle.Page3(),
                        //new Pages.PageSingle.Page4(),
                        new Pages.PageSingle.Page5(),
                        new Pages.PageSingle.Page6(),
                    }
                },
                new PageItem {
                    Page = new Page11(),
                    Alternatives = new List<Page>
                    {
                        new Page2(),
                        new Page15(),
                        new Page20(),  
                    }
                },
            };

        private List<PageItem> Items_test = new List<PageItem>
        {
            new PageItem {
                    Page = new Page2(),
                },
        };

        private void OnScheme5()
        {
#if DEBUG
            CreateCrossWindow(Items1, true);
            //CreateCrossWindow(Items_test, true);
            //CreatePageWindow("警情布局1", 0, false);
#else
            CreateCrossWindow(Items1, true);
#endif
        }
    }
}